home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / mui / bcc_src.lha / Parser / ParseBH.cpp < prev    next >
C/C++ Source or Header  |  1998-03-15  |  6KB  |  366 lines

  1. #include "ParseBH.h"
  2. #include "MethodDef.h"
  3. #include "ClassDef.h"
  4. #include "Global.h"
  5.  
  6. #include "VarDef.h"
  7.  
  8. #include <string.h>
  9.  
  10. short ParseBH::Start( void )
  11. {
  12.  
  13.     IfDefBeg();
  14.     
  15.     ins_every.Insert( ofh );
  16.     ins_header.Insert( ofh );
  17.  
  18.     UpdateLineNo();
  19.  
  20.     switches = 0;
  21.  
  22.     while( 1 ) {
  23.     
  24.         GetToken();
  25.         if( !TokLen ) break;
  26.  
  27.         SetCType();
  28.         
  29.         if( TokLen == 5 && !strncmp( "Class", Tok, 5 ) ) {
  30.             if( !DoClass() ) {
  31.                 if( !ErrorBuf ) Error( 12 );
  32.                 return 0;
  33.             }
  34.         } else
  35.         if( TokLen == 7 && !strncmp( "include", Tok, 7 ) ) {
  36.             if( !DoHeader() ) {
  37.                 if( !ErrorBuf ) Error( 12 );
  38.                 return 0;
  39.             }
  40.         } else
  41.         if( TokLen == 10 && !strncmp( "selfcreate", Tok, 10 ) ) {
  42.             switches |= SW_SELFCREATE;
  43.             StopCopy();
  44.             StartCopy();
  45.         } else switches = 0;
  46.  
  47.     }
  48.  
  49.     IfDefEnd();
  50.  
  51.     return 1;
  52.  
  53. }
  54.  
  55. short ParseBH::DoClass( void )
  56. {
  57.  short BLevel;
  58.  
  59.     StopCopy();
  60.  
  61.     GetToken();
  62.     if( !TokLen ) return 0;
  63.         
  64.     if( strlen( (char*)sfname ) != TokLen || strncmp( (char*)sfname, Tok, strlen( (char*)sfname ) )  ) {
  65.         Error( 1 );
  66.         return 0;
  67.     }
  68.  
  69.     ClassDef *cd;
  70.     
  71.     cd = new ClassDef( Tok, TokLen );
  72.  
  73.     if( !cd ) { printf( "Memory failture\n" ); exit( 10 ); }
  74.  
  75.     cd->sw = switches;
  76.     ClassList.AddTail( (Family*)cd );
  77.  
  78.     classdef = cd;
  79.     cd->type = ctype;
  80.  
  81.     BLevel = MBracket;
  82.  
  83.     GetToken();
  84.     if( !TokLen ) return 0;
  85.  
  86.     /* Super class */
  87.     if( chcmp( ':' ) ) {
  88.  
  89.         GetToken();
  90.         if( !TokLen ) return 0;
  91.  
  92.         if( TokLen == 7 && !strncmp( "private", Tok, 7 ) ) {
  93.  
  94.             cd->superpriv = 1;
  95.  
  96.             GetToken();
  97.             if( !TokLen ) return 0;
  98.  
  99.         }
  100.  
  101.         memcpy( cd->PSuper, Tok, min( TokLen, 30 ) );
  102.         cd->PSuper[ min( TokLen, 30 ) ] = 0;
  103.  
  104.         GetToken();
  105.         if( !TokLen ) return 0;
  106.  
  107.     } else {
  108.         switch( *cd->type ) {
  109.             case 'B':
  110.                 strcpy( cd->PSuper, "\"rootclass\"" );
  111.                 break;
  112.             default:
  113.                 strcpy( cd->PSuper, "MUIC_Notify" );
  114.         }
  115.     }
  116.  
  117.     if( !chcmp( '{' ) ) {
  118.         Error( 2 );
  119.         return 0;
  120.     }
  121.     
  122.     fprintf( ofh, "typedef struct {\n" ); 
  123.  
  124.     StartCopy();
  125.  
  126.     short nods = 0;
  127.     short swtch = 0;
  128.  
  129.     InitClassPtrDef();
  130.  
  131.     UpdateLineNo();
  132.  
  133.     while( 1 ) {
  134.     
  135.         next:    
  136.         GetToken();
  137.         if( !TokLen ) return 0;
  138.  
  139.         if( chcmp( '}' ) && MBracket == BLevel ) break;
  140.  
  141.  
  142.  
  143.         /* shortcuts to data variables */
  144.         if( ( ( chcmp( ',' ) || chcmp( ';' ) || chcmp( ':' ) || chcmp( '[') ) || chcmp( '=' ) ) && PrevTok[0] && (PrevType == ALN) ) {
  145.             String opbuf( "(data->" );
  146.             opbuf += PrevTok;
  147.             opbuf += ")";
  148.  
  149.             String AttrName( PrevTok );
  150.  
  151.             /* Simple Set Get Init variables */
  152.             if( chcmp( ':' ) ) {
  153.  
  154.                 StopCopy();
  155.  
  156.                 GetToken();
  157.                 if( !TokLen ) return 0;
  158.  
  159.                 if( TokType != ALN || TokLen > 3 ) {
  160.                     Error( 9 );
  161.                     return 0;
  162.                 }
  163.  
  164.                 VarDef *vd = new VarDef( (char*)AttrName, Tok, TokLen, cd, swtch|SW_SIMPLE );
  165.                 cd->Var.AddTail( (Family*)vd );
  166.  
  167.                 StartCopy();
  168.  
  169.                 GetToken();
  170.                 if( !TokLen ) return 0;
  171.  
  172.             }
  173.  
  174.             String defval;
  175.  
  176.             /* default values */
  177.             if( chcmp( '=' ) ) {
  178.  
  179.  
  180.                 StopCopy();
  181.  
  182.                 GetToken();
  183.                 if( !TokLen ) return 0;
  184.  
  185.                 StartCopy();
  186.  
  187.                 defval.Copy( Tok, TokLen );
  188.  
  189.                 GetToken();
  190.                 if( !TokLen ) return 0;
  191.  
  192.             }
  193.  
  194.             cd->rep.Add( (char*)AttrName, 0, (char*)opbuf, 0, (char*)defval );
  195.  
  196.  
  197.         }
  198.  
  199.  
  200.         /* Class Ptr definition */
  201.         short ret;
  202.         ret = ClassPtrDefinition( &cd->clref );
  203.         if( ret < 0 ) return 0;
  204.         if( ret ) goto next;
  205.  
  206.         /* Method definition */
  207.         if( TokLen == 6 && !strncmp( Tok, "Method", 6 ) ) {
  208.             StopCopy();
  209.  
  210.             GetToken();
  211.             if( !TokLen ) return 0;
  212.  
  213.             if( TokType != ALN ) {
  214.                 Error( 4 );
  215.                 return 0;
  216.             }            
  217.  
  218.             MethodDef *md;
  219.             md = new MethodDef( Tok, TokLen, cd, swtch );
  220.  
  221.             short CLevel = CBracket;
  222.             
  223.             GetToken();
  224.             if( !TokLen ) return 0;
  225.             
  226.             if( !chcmp( '(' ) ) {
  227.                 Error( 5 );
  228.                 return 0;
  229.             }
  230.             
  231.             while( 1 ) {
  232.  
  233.                 GetToken();
  234.                 if( !TokLen ) return 0;
  235.                 
  236.                 if( chcmp( ')' ) && CBracket == CLevel ) break;
  237.         
  238.             }
  239.  
  240.             GetToken();
  241.             if( !TokLen ) return 0;
  242.             
  243.             if( !chcmp( ';' ) ) {
  244.                 Error( 3 );
  245.                 return 0;
  246.             }
  247.             
  248.             cd->AddTail( (Family*)md );
  249.             
  250.             StartCopy();
  251.         } else {
  252.         /* Attribute definition */
  253.         if( TokLen == 9 && !strncmp( "Attribute", Tok, 9 ) ) {
  254.             StopCopy();
  255.  
  256.             GetToken();
  257.             if( !TokLen ) return 0;
  258.  
  259.             if( TokType != ALN ) {
  260.                 Error( 4 );
  261.                 return 0;
  262.             }            
  263.  
  264.             GetToken();
  265.             if( !TokLen ) return 0;
  266.  
  267.             VarDef *vd1;
  268.  
  269.             if( chcmp( ':' ) ) {
  270.  
  271.                 String AttrName( PrevTok );
  272.  
  273.                 GetToken();
  274.                 if( !TokLen ) return 0;
  275.  
  276.                 if( TokType != ALN || TokLen > 3 ) {
  277.                     Error( 9 );
  278.                     return 0;
  279.                 }
  280.  
  281.                 vd1 = new VarDef( (char*)AttrName, Tok, TokLen, cd, swtch );
  282.  
  283.                 GetToken();
  284.                 if( !TokLen ) return 0;
  285.  
  286.             } else {
  287.                 vd1 = new VarDef( PrevTok, "S", 1, cd, swtch );
  288.             }
  289.  
  290.             cd->Var.AddTail( (Family*)vd1 );
  291.             
  292.             if( !chcmp( ';' ) ) {
  293.                 Error( 3 );
  294.                 return 0;
  295.             }
  296.             
  297.             StartCopy();
  298.  
  299.         }
  300.         }
  301.  
  302.         /* Virtual methods/attributes */
  303.         if( TokLen == 7 && !strncmp( "virtual", Tok, 7 ) ) {
  304.             StopCopy();
  305.             StartCopy();
  306.             swtch |= SW_VIRTUAL;
  307.             goto next;
  308.         } else swtch = 0;
  309.  
  310.     }
  311.  
  312.     StopCopy();
  313.  
  314.     GetToken();
  315.     if( !TokLen ) return 0;
  316.         
  317.     if( TokLen != 1 || *Tok != ';' ) {    
  318.         Error( 3 );
  319.         return 0;
  320.     }
  321.     
  322.     fprintf( ofh, "} %sData;\n\n/* Method Tags */\n", cd->Name );
  323.     
  324.     FScan( MethodDef, child, cd ) {
  325.         fprintf( ofh, "#define %s 0x%lx\n", child->FullName(), child->GetTagVal() );
  326.     }
  327.  
  328.     fprintf( ofh, "\n" );
  329.  
  330.     FScan( VarDef, child1, &(cd->Var) ) {
  331.         fprintf( ofh, "#define %s 0x%lx\n", child1->FullName(), child1->GetTagVal() );
  332.     }
  333.  
  334.     switch( *cd->type ) {
  335.         case 'B':
  336.             fprintf( ofh, "\nextern struct IClass *cl_%s;\n", cd->Name );
  337.             if( cd->sw & SW_SELFCREATE ) {
  338.                 fprintf( ofh, "Object *%s_New( unsigned long pad, unsigned long tags, ... );\n", cd->Name );
  339.                 fprintf( ofh, "#define %sObject %s_New( 0\n", cd->Name, cd->Name );
  340.             } else {
  341.                 fprintf( ofh, "#define %sObject NewObject( cl_%s, NULL\n", cd->Name, cd->Name );
  342.                 fprintf( ofh, "struct IClass *%s_Create( void );\n", cd->Name );
  343.             }
  344.             break;
  345.         default:
  346.             fprintf( ofh, "\nextern struct MUI_CustomClass *cl_%s;\n", cd->Name );
  347.             if( cd->sw & SW_SELFCREATE ) {
  348.                 fprintf( ofh, "Object *%s_New( unsigned long pad, unsigned long tags, ... );\n", cd->Name );
  349.                 fprintf( ofh, "#define %sObject %s_New( 0\n", cd->Name, cd->Name );
  350.             } else {
  351.                 fprintf( ofh, "#define %sObject NewObject( cl_%s->mcc_Class, NULL\n", cd->Name, cd->Name );
  352.                 fprintf( ofh, "struct MUI_CustomClass *%s_Create( void );\n", cd->Name );
  353.             }
  354.  
  355.     }
  356.  
  357.     StartCopy();
  358.  
  359.     if( cd->CheckDoubleTags() ) return 0;
  360.  
  361.     UpdateLineNo();
  362.         
  363.       return 1;
  364.     
  365. }
  366.